﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ultima.Xna.Extensions;

namespace Ultima.Xna.Graphics.UI.Gumps
{
    public interface IGumpEntry { }

    public class GumpEntry : UINode, IGumpEntry
    {
        protected Effect GumpEffect;

        private ICameraService _cameraService;

        public ICameraService CameraManager
        {
            get { return _cameraService; }
        }

        public GumpEntry(Game game)
            : base(game)
        {
            _cameraService = game.Services.GetService<ICameraService>(true);
        }

        public override void Initialize()
        {
            base.Initialize();

            GumpEffect = Game.Content.Load<Effect>("Shaders\\Gumps");
        }

        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);

            Children.Sort();

            //Render backgrounds first
            for (int i = 0; i < Children.Count; i++)
            {
                if(Children[i] is GumpBackground)
                {
                    Children[i].Draw(gameTime);
                }
            }

            //Render alpha regions second
            for (int i = 0; i < Children.Count; i++)
            {
                if (Children[i] is GumpAlpha)
                {
                    Children[i].Draw(gameTime);
                }
            }

            //Render everything else last
            for (int i = 0; i < Children.Count; i++)
            {
                if (!(Children[i] is GumpAlpha) &&
                    !(Children[i] is GumpBackground))
                {
                    Children[i].Draw(gameTime);
                }
            }
        }
    }
}
